package com.example.digitaldiary;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends CursorAdapter {

	private Activity activity;
	private DataHelper helper;
	private LayoutInflater inflater;

	public LazyAdapter(Activity activity, Cursor cursor, DataHelper helper) {
		super(activity, cursor, true);
		this.activity = activity;
		this.helper = helper;
		inflater = (LayoutInflater) activity.getLayoutInflater();
	}

	@Override
	public void bindView(View arg0, Context arg1, Cursor arg2) {
		Holder holder = (Holder) arg0.getTag();
		holder.populateFrom(arg2, helper);
	}

	@Override
	public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
		View row = inflater.inflate(R.layout.list_row, arg2, false);
		row.setTag(new Holder(row));
		return row;
	}

}

class Holder {
	TextView dateBig;
	TextView notes;
	TextView timeSmall;
	ImageView smiley_image;

	public Holder(View view) {
		dateBig = (TextView) view.findViewById(R.id.date);
		notes = (TextView) view.findViewById(R.id.notes);
		timeSmall = (TextView) view.findViewById(R.id.time);
		smiley_image = (ImageView) view.findViewById(R.id.list_image);
	}

	public void populateFrom(Cursor cursor, DataHelper helper) {
		String[] dateTime = helper.getDateCreated(cursor).split(" ");
		dateBig.setText(dateTime[0]);
		timeSmall.setText(dateTime[1]);
		notes.setText(helper.getNote(cursor));
		smiley_image.setImageResource(Integer.parseInt(helper.getSmiley(cursor)));
	}
}
